package com.iceclean.daoImpl;

import com.iceclean.dao.PostBrowsingDao;
import com.iceclean.po.Post;
import com.iceclean.po.PostBrowsing;
import com.iceclean.util.JdbcUtil;
import jdk.nashorn.internal.scripts.JD;

import java.util.List;

/**
 * @author : Ice'Clean
 * @date : 2021-05-08
 */
public class PostBrowsingDaoImpl implements PostBrowsingDao {

    @Override
    public int createPostBrowsing(PostBrowsing postBrowsing) {
        return JdbcUtil.insert("t_post_browsing", postBrowsing);
    }

    @Override
    public PostBrowsing findPostBrowsingById(int postBrowsingId) {
        List<PostBrowsing> postBrowsing = JdbcUtil.query("select * from t_post_browsing where browsing_id = ?", PostBrowsing.class, postBrowsingId);
        return postBrowsing.size() == 0 ? null : postBrowsing.get(0);
    }

    /**
     * 默认降序排序，最新的浏览记录在最前面
     * @param userId 所属用户
     * @param page 页数
     * @param num 每页的数目
     * @return 符合条件的浏览记录
     */
    @Override
    public List<PostBrowsing> findBrowsingByPage(int userId, int page, int num) {
        List<PostBrowsing> postBrowsingList = JdbcUtil.query("select * from t_post_browsing where user_id = ?  order by create_date desc limit " + (page-1)*num + "," + num, PostBrowsing.class, userId);
        return postBrowsingList.size() == 0 ? null : postBrowsingList;
    }

    @Override
    public int getNumberOfAllBrowsing(int userId) {
        return JdbcUtil.query("select * from t_post_browsing where user_id = " + userId, PostBrowsing.class).size();
    }

    @Override
    public boolean deleteUserBrowsing(int userId) {
        return JdbcUtil.delete("t_post_browsing", "user_id = " + userId);
    }

    @Override
    public boolean deleteUserBrowsingByPostId(int userId, int postId) {
        return JdbcUtil.delete("t_post_browsing", "user_id = " + userId + " and post_id = " + postId);
    }
}
